package com.common.utils;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @作者: 林江
 * @创建时间: 2023/11/18
 * @功能:
 */
public class TreeHelper {

    private String idName = "id";
    private String parentIdName = "parentId";
    private String childName = "children";

    public static TreeHelperBuilder builder() {
        return new TreeHelperBuilder();
    }

    public static class TreeHelperBuilder {
        TreeHelper treeHelper = new TreeHelper();

        public TreeHelperBuilder idName(String idName) {
            treeHelper.idName = idName;
            return this;
        }

        public TreeHelperBuilder parentIdName(String parentIdName) {
            treeHelper.parentIdName = parentIdName;
            return this;
        }

        public TreeHelperBuilder childName(String childName) {
            treeHelper.childName = childName;
            return this;
        }

        public TreeHelper build() {
            return treeHelper;
        }
    }

    public <T> List<T> tree(List<T> data) {
        return this.tree(null, data);
    }

    public <T> T singleTree(T t, List<T> data) {
        List<T> child = this.tree(t, data);
        BeanUtil.setFieldValue(t, this.childName, child);
        return t;
    }

    public <T, P> List<T> tree(P p, List<T> data) {
        return data.stream().filter(t -> filter(t, p)).map(pp -> {
            List<T> child = tree(pp, data);
            BeanUtil.setFieldValue(pp, this.childName, child);
            return pp;
        }).collect(Collectors.toList());
    }

    public <T, P> Boolean filter(T var1, P var2) {
        Class<?> pClazz = var1.getClass();
        Field parentfield = null;
        try {
            parentfield = pClazz.getDeclaredField(this.parentIdName);
        } catch (NoSuchFieldException e) {
            try {
                parentfield = pClazz.getSuperclass().getDeclaredField(this.parentIdName);
            } catch (NoSuchFieldException ex) {
                e.printStackTrace();
                return false;
            }
        }


        Field idField = null;
        if (var2 != null) {
            Class<?> cClaszz = var2.getClass();
            try {
                idField = cClaszz.getDeclaredField(this.idName);
            } catch (NoSuchFieldException e) {
                try {
                    idField = cClaszz.getSuperclass().getDeclaredField(this.idName);
                } catch (NoSuchFieldException ex) {
                    e.printStackTrace();
                    return false;
                }
            }
        }

        Object parentIdValue = BeanUtil.getFieldValue(var1, parentfield.getName());
        Object idValue = idField == null ? null : BeanUtil.getFieldValue(var2, idField.getName());
        // 第一次遍历，只获取父级（最顶级）
        if (var2 == null && (parentIdValue == null || parentIdValue.toString().equals("0"))) {
            return true;
        }
        // 第二次遍历，获取子集
        else if (idValue != null && idValue.toString().equals(parentIdValue.toString())) {
            return true;
        }
        return false;
    }

    /**
     * 模拟数据
     *
     * @return
     */
    public List<Map> getList() {
        String str = "[\n" + "    {\n" + "        \"id\": \"1\",\n" + "        \"createTime\": \"2023-05-04 02:46:47\",\n" + "        \"updateTime\": \"2023-05-06 09:33:53\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 0,\n" + "        \"title\": \"系统管理\",\n" + "        \"component\": \"system\",\n" + "        \"sortValue\": 1,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"2\",\n" + "        \"createTime\": \"2023-05-04 02:47:13\",\n" + "        \"updateTime\": \"2023-05-06 09:33:57\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 1,\n" + "        \"title\": \"用户管理\",\n" + "        \"component\": \"sysUser\",\n" + "        \"sortValue\": 1,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"3\",\n" + "        \"createTime\": \"2023-05-04 02:47:19\",\n" + "        \"updateTime\": \"2023-05-06 09:33:58\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 1,\n" + "        \"title\": \"角色管理\",\n" + "        \"component\": \"sysRole\",\n" + "        \"sortValue\": 2,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"4\",\n" + "        \"createTime\": \"2023-05-04 02:47:26\",\n" + "        \"updateTime\": \"2023-05-06 09:33:58\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 1,\n" + "        \"title\": \"菜单管理\",\n" + "        \"component\": \"sysMenu\",\n" + "        \"sortValue\": 3,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"5\",\n" + "        \"createTime\": \"2023-05-05 04:25:07\",\n" + "        \"updateTime\": \"2023-05-15 07:37:36\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 0,\n" + "        \"title\": \"基础数据管理\",\n" + "        \"component\": \"base\",\n" + "        \"sortValue\": 2,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"6\",\n" + "        \"createTime\": \"2023-05-05 04:27:20\",\n" + "        \"updateTime\": \"2023-05-15 07:44:33\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 5,\n" + "        \"title\": \"商品单位\",\n" + "        \"component\": \"productUnit\",\n" + "        \"sortValue\": 2,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"7\",\n" + "        \"createTime\": \"2023-05-15 07:38:09\",\n" + "        \"updateTime\": \"2023-05-15 07:52:22\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 5,\n" + "        \"title\": \"地区管理\",\n" + "        \"component\": \"region\",\n" + "        \"sortValue\": 1,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"8\",\n" + "        \"createTime\": \"2023-05-15 07:38:47\",\n" + "        \"updateTime\": \"2023-05-15 07:52:23\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 0,\n" + "        \"title\": \"商品管理\",\n" + "        \"component\": \"product\",\n" + "        \"sortValue\": 3,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"9\",\n" + "        \"createTime\": \"2023-05-15 07:39:04\",\n" + "        \"updateTime\": \"2023-05-15 07:52:23\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 8,\n" + "        \"title\": \"分类管理\",\n" + "        \"component\": \"category\",\n" + "        \"sortValue\": 1,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"10\",\n" + "        \"createTime\": \"2023-05-15 07:39:14\",\n" + "        \"updateTime\": \"2023-05-15 07:52:24\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 8,\n" + "        \"title\": \"品牌管理\",\n" + "        \"component\": \"brand\",\n" + "        \"sortValue\": 2,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"11\",\n" + "        \"createTime\": \"2023-05-15 07:39:23\",\n" + "        \"updateTime\": \"2023-05-15 07:52:26\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 8,\n" + "        \"title\": \"分类品牌\",\n" + "        \"component\": \"categoryBrand\",\n" + "        \"sortValue\": 3,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"12\",\n" + "        \"createTime\": \"2023-05-15 07:39:34\",\n" + "        \"updateTime\": \"2023-05-15 07:52:25\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 8,\n" + "        \"title\": \"商品规格\",\n" + "        \"component\": \"productSpec\",\n" + "        \"sortValue\": 4,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"13\",\n" + "        \"createTime\": \"2023-05-15 07:39:42\",\n" + "        \"updateTime\": \"2023-05-15 07:52:26\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 8,\n" + "        \"title\": \"商品列表\",\n" + "        \"component\": \"product\",\n" + "        \"sortValue\": 5,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"14\",\n" + "        \"createTime\": \"2023-05-15 07:41:00\",\n" + "        \"updateTime\": \"2023-05-15 08:00:52\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 0,\n" + "        \"title\": \"会员管理\",\n" + "        \"component\": \"users\",\n" + "        \"sortValue\": 4,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"15\",\n" + "        \"createTime\": \"2023-05-15 07:41:24\",\n" + "        \"updateTime\": \"2023-05-15 07:55:28\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 14,\n" + "        \"title\": \"会员列表\",\n" + "        \"component\": \"userInfo\",\n" + "        \"sortValue\": 1,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"16\",\n" + "        \"createTime\": \"2023-05-15 07:42:23\",\n" + "        \"updateTime\": \"2023-05-15 07:52:29\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 0,\n" + "        \"title\": \"订单管理\",\n" + "        \"component\": \"order\",\n" + "        \"sortValue\": 5,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"17\",\n" + "        \"createTime\": \"2023-05-15 07:42:43\",\n" + "        \"updateTime\": \"2023-05-15 07:52:29\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 16,\n" + "        \"title\": \"订单列表\",\n" + "        \"component\": \"orderInfo\",\n" + "        \"sortValue\": 1,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"18\",\n" + "        \"createTime\": \"2023-05-15 07:43:05\",\n" + "        \"updateTime\": \"2023-05-30 02:07:50\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 16,\n" + "        \"title\": \"订单统计\",\n" + "        \"component\": \"orderStatistics\",\n" + "        \"sortValue\": 2,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    },\n" + "    {\n" + "        \"id\": \"21\",\n" + "        \"createTime\": \"2023-06-02 01:04:13\",\n" + "        \"updateTime\": \"2023-06-02 01:04:21\",\n" + "        \"isDeleted\": 0,\n" + "        \"parentId\": 1,\n" + "        \"title\": \"操作日志\",\n" + "        \"component\": \"sysOperLog\",\n" + "        \"sortValue\": 4,\n" + "        \"status\": 1,\n" + "        \"children\": null\n" + "    }\n" + "]";
        return JSON.parseArray(str, Map.class);
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        TreeHelper helper = new TreeHelper();
        List<Map> trees = helper.tree(null, helper.getList());
        System.out.println(trees);
    }
}
